<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class ProductCategory extends Model
{
    //
    protected $table = "product_category";
    public $timestamps = false;

    /**
     * 根据id获取分类信息
     * @param $id
     * @return mixed
     */
    public static function getCategoryInfo($id)
    {
        $res = self::where('id',$id)->first();
        return $res;
    }

    /**
     * 获取所有一级分类信息
     * @return mixed
     */
    public static function getOneInfo()
    {
        $res = self::where('level',1)->where('status',1)->get()->toArray();
        return $res;
    }

    /**
     * 获取所有二级分类信息
     * @return mixed
     */
    public static function getTwoInfo()
    {
        $res = self::where('level',2)->where('status',1)->get();
        return $res;
    }

    /**
     * 获取一级分类下的二级分类1
     * @param $id
     * @return mixed
     */
    public static function getOneTwoInfo($id) {
        $res = self::where('parent_id',$id)->where('level',2)->where('status',1)->get()->toArray();
        return $res;
    }

    /**
     * 编辑分类项
     * @param $data
     * @return mixed
     */
    public static function updateProductCategory($data)
    {
        if(empty($data['path'])){
            $res = self::where('id',$data['id'])->update([
                'parent_id'=>$data['parent_id'],
                'name'=>$data['name']
            ]);
        }else{
            $res = self::where('id',$data['id'])->update([
                'parent_id'=>$data['parent_id'],
                'name'=>$data['name'],
                'picture'=>$data['path']
            ]);
        }
        return $res;
    }

    public static function addProductCategory($data)
    {
        $res = self::insert([
            'parent_id'=>$data['parent_id'],
            'name'=>$data['name'],
            'picture'=>$data['path'],
            'level'=>$data['parent_id']==0?1:2
        ]);
        return $res;
    }
}